package com.yyh.synctool.client.controller;

import com.yyh.common.Common;
import com.yyh.common.StringCommon;
import com.yyh.result.JsonResult;
import com.yyh.synctool.client.component.Setting;
import com.yyh.synctool.client.constant.DB;
import com.yyh.synctool.client.pojo.vo.DBConfigParam;
import com.yyh.synctool.client.util.DBTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xuan
 * @date 2021/12/3
 */
@RestController
@RequestMapping("/setting")
public class SettingController
{
    private Setting setting;

    @Autowired
    public SettingController(Setting setting) {
        this.setting = setting;
    }

    /** 测试数据库连接 */
    @PostMapping("/test-connection")
    public JsonResult testConnection(@RequestBody DBConfigParam dbConfigParam) {
        boolean status = DBTest.test(dbConfigParam.getType(), dbConfigParam.getUrl(),
                dbConfigParam.getUsername(), dbConfigParam.getPassword());

        HashMap<String, Object> map = new HashMap<>();
        map.put("status", status);
        return JsonResult.getSuccess(map);
    }

    /** 展示数据库连接配置 */
    @GetMapping("/show-connection")
    public JsonResult showConnection() {
        DB defaultDB = DB.getInstance("MySQL");
        //String type = StringCommon.isEmpty(setting.getType()) ? defaultDB.getType() : setting.getType();
        String host = StringCommon.isEmpty(setting.getHost()) ? "localhost" : setting.getHost();
        String port = StringCommon.isEmpty(setting.getPort()) ? defaultDB.getDefaultPort() : setting.getPort();
        String username = setting.getUsername();
        String password = setting.getPassword();
        String databse = setting.getDatabase();
        String url = StringCommon.isEmpty(setting.getUrl()) ? defaultDB.getPrefixUrl() : setting.getUrl();
        HashMap<String, Object> dbs = new HashMap<>();
        for (String dbType : DB.types()) {
            dbs.put(dbType, DB.getInstance(dbType).getPrefixUrl());
        }

        HashMap<String, Object> config = new HashMap<>();
        config.put("types", DB.types());
        config.put("dbs", dbs);
        //config.put("type", type);
        config.put("host", host);
        config.put("port", port);
        config.put("username", username);
        config.put("password", password);
        config.put("database", databse);
        config.put("url", url);

        return JsonResult.getSuccess(config);
    }

    /** 保存数据库连接配置 */
    @PostMapping("/save-connection")
    public JsonResult saveConnection(@RequestBody DBConfigParam dbConfigParam) {
        Map<String, String> config = Common.stringifyMapValue(Common.toMap(dbConfigParam));
        setting.setType(config.get("type"))
                .setHost(config.get("host"))
                .setPort(config.get("port"))
                .setUsername(config.get("username"))
                .setPassword(config.get("password"))
                .setDatabase(config.get("database"))
                .setUrl(config.get("url"));
        setting.saveDBConfig();
        return JsonResult.getSuccess(config);
    }
}
